package com.notify;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import android.app.Activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class ViewActivity extends Activity {

	String currentId = "";
	String idToDelete = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.view);

		Intent i = getIntent();

		// getting from intent
		this.currentId = i.getStringExtra("id");

		String titleText = i.getStringExtra("title");
		String contentText = i.getStringExtra("content");

		((TextView) findViewById(R.id.title)).setText(titleText);
		((TextView) findViewById(R.id.content)).setText(contentText);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mini_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.delete:
			idToDelete = this.currentId;
			new DeleteProccedure().execute();
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public NodeList deleteNotify(String id, Context con) {
		Tools tools = new Tools();
		String username = tools.getData(con, "username", "");
		String password = tools.getData(con, "password", "");
		String server = tools.getData(con, "server", "");

		try {

			XMLParser parser = new XMLParser();
			String fullUrl = server + "?action=delete&username=" + username
					+ "&password=" + password + "&messageId=" + id;

			Log.v("URL", fullUrl);

			String xml = parser.getXmlFromUrl(fullUrl); // getting XML

			Document doc = parser.getDomElement(xml); // getting DOM element

			NodeList nl = doc.getElementsByTagName(getResources().getString(
					R.string.key_item));
			return nl;

		} catch (Exception e) {

		}
		return null;
	}

	class DeleteProccedure extends AsyncTask<String, String, String> {

		private ProgressDialog pDialog;

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(ViewActivity.this);
			pDialog.setMessage("Deleting...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {

			try {

				Log.v("ID to delete", idToDelete);

				deleteNotify(idToDelete, getApplicationContext());

			} catch (Exception e) {

				Intent in = new Intent(getApplicationContext(),
						LoginActivity.class);
				finish();
				startActivity(in);
				Toast.makeText(getApplicationContext(),
						"Error deleting message ", Toast.LENGTH_LONG).show();

			}
			return null;
		}

		protected void onPostExecute(String file_url) {

			pDialog.dismiss();
			// updating UI from Background Thread

			runOnUiThread(new Runnable() {
				public void run() {
					Intent in = new Intent(getApplicationContext(),
							NotifierActivity.class);
					finish();
					startActivity(in);
				}
			});

		}
	}
}
